package main

import (
	"log"
	"net/http"
	database "web2/pkg/db"
	"web2/pkg/handlers"
	"web2/pkg/repositories"
	"web2/pkg/services"
	_ "web2/pkg/services"

	//"xorm.io/xorm"

	"github.com/gin-gonic/gin"
	_ "modernc.org/sqlite" // SQLite驱动
)

func main() {

	database.Init("warehouse.db")

	productRepo := repositories.NewProductRepository()
	productService := services.NewProductService(productRepo)
	productHandler := handlers.NewProductHandler(productService)

	gin.SetMode(gin.ReleaseMode)
	r := gin.Default()
	r.Use(gin.Recovery())
	r.Static("/static", "./static")
	r.LoadHTMLFiles("static/templates/index.html")
	r.GET("/", func(context *gin.Context) {
		context.HTML(http.StatusOK, "index.html", "alksl")
	})
	r.GET("/products", productHandler.GetProducts)
	r.GET("/products/:id", productHandler.GetProduct)
	r.POST("/products", productHandler.CreateProduct)
	r.PUT("/products/:id", productHandler.UpdateProduct)
	r.DELETE("/products/:id", productHandler.DeleteProduct)
	r.GET("/health", func(c *gin.Context) {
		c.JSON(200, gin.H{"status": "healthy"})
	})

	// 启动服务器
	log.Printf("🚀 服务器启动在端口 %s", "8080")
	if err := r.Run(":8080"); err != nil {
		log.Fatal("服务器启动失败:", err)
	}
}
